package cn.neusoft.service.impl;

import cn.neusoft.bean.DeliveryAddress;
import cn.neusoft.dao.IDeliveryAddressDao;
import cn.neusoft.dao.impl.DeliveryAddressDao;
import cn.neusoft.query.BaseQuery;
import cn.neusoft.query.DeliveryAddressQuery;
import cn.neusoft.service.IDeliveryAddressService;
import cn.neusoft.vo.PageBean;
import cn.neusoft.vo.ResultBean;
import com.alibaba.druid.util.StringUtils;

import java.util.List;

public class DeliveryAddressService implements IDeliveryAddressService {
    private IDeliveryAddressDao deliveryAddressDao;

    public DeliveryAddressService() {
        deliveryAddressDao = new DeliveryAddressDao();
    }

    @Override
    public ResultBean add(DeliveryAddress deliveryAddress) throws Exception {
        if (deliveryAddress == null ||
                StringUtils.isEmpty(deliveryAddress.getContactName())||
                deliveryAddress.getContactSex() == null||
                StringUtils.isEmpty(deliveryAddress.getContactTel())||
                StringUtils.isEmpty(deliveryAddress.getAddress())||
                StringUtils.isEmpty(deliveryAddress.getUserId())
        ){
            return ResultBean.ERROR(10001,"params null");
        }
        int i = deliveryAddressDao.add(deliveryAddress);
        return i>0?ResultBean.SUCCESS():ResultBean.ERROR("add deliveryAddress fail");
    }

    @Override
    public ResultBean update(DeliveryAddress deliveryAddress) throws Exception {
        // 参数校验
        if (deliveryAddress == null ||
                deliveryAddress.getDaId()==null||
                StringUtils.isEmpty(deliveryAddress.getContactName())||
                deliveryAddress.getContactSex() == null||
                StringUtils.isEmpty(deliveryAddress.getContactTel())||
                StringUtils.isEmpty(deliveryAddress.getAddress())||
                StringUtils.isEmpty(deliveryAddress.getUserId())
        ){
            return ResultBean.ERROR(10001,"params null");
        }
        int i = deliveryAddressDao.update(deliveryAddress);
        return i>0?ResultBean.SUCCESS():ResultBean.ERROR("update deliveryAddress fail");
    }

    @Override
    public ResultBean delete(Integer id) throws Exception {
        if (id == null){
            return ResultBean.ERROR(10001,"params null");
        }
        int i = deliveryAddressDao.delete(id);
        return i>0?ResultBean.SUCCESS():ResultBean.ERROR("delete deliveryAddress fail");
    }

    @Override
    public ResultBean getById(Integer id) throws Exception {
        if (id == null){
            return ResultBean.ERROR(10001,"params null");
        }
        DeliveryAddress byId = deliveryAddressDao.getById(id);
        return byId!=null?ResultBean.SUCCESS(byId):ResultBean.ERROR("get deliveryAddress fail");
    }

    @Override
    public ResultBean getAll() throws Exception {
        List<DeliveryAddress> deliveryAddressList = deliveryAddressDao.getAll();
        return deliveryAddressList!=null?ResultBean.SUCCESS(deliveryAddressList):ResultBean.ERROR("get all deliveryAddress fail");
    }

    @Override
    public ResultBean getByPage(BaseQuery baseQuery) throws Exception {
        if (baseQuery == null){
            baseQuery = new DeliveryAddressQuery();
        }
        PageBean<DeliveryAddress> pageBean = new PageBean<>();
        //查询总记录数
        int count = deliveryAddressDao.getCount(baseQuery);
        pageBean.setTotalRows(count);

        //计算总页数
        if (count%baseQuery.getPageCount()==0){
            pageBean.setTotalPages(count/baseQuery.getPageCount());
        }else {
            pageBean.setTotalPages(count/baseQuery.getPageCount()+1);
        }

        //查询集合
        List<DeliveryAddress> deliveryAddressList = deliveryAddressDao.getByPage(baseQuery);
        pageBean.setData(deliveryAddressList);

        return ResultBean.SUCCESS(pageBean);
    }

    //批量删除
    @Override
    public ResultBean batchDel(List<Integer> ids) throws Exception {
        return null;
    }
}
